package com.znsd.zl.app.ocr.service.impl;

import com.baidu.aip.ocr.AipOcr;
import com.znsd.zl.app.ocr.entity.IdCardEntity;
import com.znsd.zl.app.ocr.service.IOcrService;
import com.znsd.zl.app.ocr.utils.IdCardUtils;
import com.znsd.zl.app.ocr.utils.JsonUtils;
import com.znsd.zl.app.ocr.utils.OcrConstants;
import com.znsd.zl.app.user.dao.IAppUserDao;
import com.znsd.zl.app.user.entity.AppUserEntity;
import com.znsd.zl.app.user.service.IAppUserService;
import com.znsd.zl.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *      ORC识别业务层接口实现类
 * </p>
 *
 * @ClassName IdCardServiceImpl
 * @Description TODO
 * @Author jaypdd
 * @Date 2020/8/29 20:33
 * @Version v1.0
 */
@Service
@Slf4j
public class OcrServiceImpl implements IOcrService {

	@Resource
	private AipOcr aipOcr;

	@Resource
	private IdCardUtils idCardUtils;

	@Resource
	private JsonUtils jsonUtil;

	@Resource
	private IAppUserService userService;

	/**
	 * 用户上传证件照实名认证
	 * @param userId     认证的用户id
	 * @param files      认证的身份证正反面照
	 */
	@Override
	public boolean realNameAuthentication(Integer userId, MultipartFile[] files) {
		// 解析身份证正反面信息
		Map<String, JSONObject> map = null;
		try {
			map = readIdCardInfo(files[0].getBytes(), files[1].getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject idCardFront = map.get("idCardFront");
		JSONObject idCardBack = map.get("idCardBack");
		System.out.println(idCardFront);
		// 身份证证件照识别成功
		if (OcrConstants.SUCCESS_IDENTITY.equals(idCardFront.getString(OcrConstants.IMAGE_STATUS))
				&& OcrConstants.SUCCESS_IDENTITY.equals(idCardBack.getString(OcrConstants.IMAGE_STATUS)) ) {
			// 封装身份证正面照信息
			IdCardEntity idCardInfo = jsonUtil.idCardFront(idCardFront);
			// 实名成功，完善用户实名信息
			AppUserEntity userEntity = new AppUserEntity();
			userEntity.setUserId(userId);
			userEntity.setName(idCardInfo.getName());
			userEntity.setIdentityCardNumber(idCardInfo.getIdentityCard());
			userEntity.setUpdateTime(new Date());
			userEntity.setGender(userService.findDictByItemText(idCardInfo.getGender()));
			userService.updateById(userEntity);
			log.info("【百度OCR】用户{}实名认证成功", userEntity.getName());
			return true;
		}else{
			log.error("【百度OCR】实名认证失败，正面状态：{}，背面状态：{}",
					idCardFront.getString(OcrConstants.IMAGE_STATUS),
					idCardBack.getString(OcrConstants.IMAGE_STATUS));
		}
		return false;
	}

	/**
	 * 读取身份证正反面信息
	 * @param frontFile 身份证正面字节数组
	 * @param backFile  身份证反面字节数组
	 * @return
	 */
	@Override
	public Map<String, JSONObject> readIdCardInfo(byte[] frontFile, byte[] backFile) {
		// 开启身份证风险类型识别
		HashMap<String, String> options = new HashMap<>(1);
		options.put("detect_risk", "true");
		// 识别正反面照信息
		JSONObject idCardFront = aipOcr.idcard(frontFile, OcrConstants.IDCARD_FRONT, options);
		JSONObject idCardBack = aipOcr.idcard(backFile, OcrConstants.IDCARD_BACK, options);
		// 真反面信息json对象键值集合
		Map<String, JSONObject> idCardInfoMap = new HashMap<>(1);
		idCardInfoMap.put("idCardFront", idCardFront);
		idCardInfoMap.put("idCardBack", idCardBack);
		return idCardInfoMap;
	}
}
